package pers.cz.netty.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cz.netty.protocal.Message;

/**
 * @program: PostGirl-panent
 * @description: 普通消息处理类
 * @author: Cheng Zhi
 * @create: 2022-10-04 23:56
 **/
@ChannelHandler.Sharable
public class MessageHandler extends SimpleChannelInboundHandler<Message> {

    private static final Logger log = LoggerFactory.getLogger(MessageHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {

        if (log.isDebugEnabled()) {
            log.debug("Server Message： type：{}, body length：{}, body：{}", message.getMsgType(), message.getContentLength(), message.getContent());
        }
    }

    /**
     * 异常处理
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
    }
}
